/**
 * 数据库Seed数据代码，仅在测试阶段使用
 *
 * @author bian
 * @createDate 2016.5.21
 */

// library module
var co = require("co");

// my module
var db = require("../models/index");
const Admin = db.models.Admin;
const Employee = db.models.Employee;
const Record = db.models.Record;
const Command = db.models.Command;

function * AdminSeed(){
    console.log("AdminSeed");
    for(var i=0;i<10;i++){
        yield Admin.create({
            account : `admin${i}`,
            password : `password${i}`
        });
    }
}

function * EmployeeSeed(){
    console.log("EmploySeed");
    for(var i=0;i<10;i++){
        yield Employee.create({
            employee_id: `20149200${i}`,
            fingerprint: Buffer.from([0xab,0xab,0x00,0x12 + i]).toString("base64"),
            psk : Buffer.from("oureda","ascii").toString("base64"),
            state : 1,
            name : "员工" + i,
            phone : "1834086100" + i,
            fingerprint_id: i
        });
    }
}

function * RecordSeed(){
    console.log("RecordSeed");
    var employees = yield Employee.findAll();
    for(var i=0;i<10;i++){
        for(var j=0;j<2;j++){
            yield Record.create({
                type: j,
                EmployeeId : employees[i].id
            });
        }
    }
}

function * CommandSeed(){
    console.log("CommandSeed");
    yield Command.create({
        psk : Buffer.from("oureda","ascii").toString("base64"),     // b3VyZWRh
        text : "working",
        data : JSON.stringify({})
    });
}

function * seed () {
    yield AdminSeed();
    // yield EmployeeSeed();
    // yield RecordSeed();
    yield CommandSeed();
}

co(function *(){
    yield db.sync({force:true});
    yield seed();
    process.exit(0);
}).catch(function(err){
    console.log(err);
});

